package com.zxhwolfe.androidCanvasTest;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

class RefreshHandler extends Handler {
	private static final long DELAY_MILLIS = 100;
	private CanvasView canvasView;
	private boolean paused = false;
	private int fallDownFlag = 0;

	public RefreshHandler(CanvasView view) {
		Log.d("handler", "init");
		canvasView = view;
	}

	@Override
	public void handleMessage(Message msg) {
		canvasView.processkey();
		//
		fallDownFlag = (fallDownFlag + 1) % 4;
		if (fallDownFlag == 0)
			canvasView.falldown();
		canvasView.invalidate();
		sleep(DELAY_MILLIS);
	}

	public void sleep(long delayMillis) {
		if (paused)
			return;
		removeMessages(0);
		sendMessageDelayed(obtainMessage(0), delayMillis);
	}

	public void pause() {
		paused = true;
	}

	public void resume() {
		paused = false;
	}
}